﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Nollie.Utilities
{
	public enum LockTypes
	{
		ReaderLock = 1,
		WriterLock = 2
	}

	public abstract class ReaderWriterLockSlimHelper : IDisposable
	{
		private ReaderWriterLockSlim _lock;
		private LockTypes _lockType;

		public ReaderWriterLockSlimHelper(ReaderWriterLockSlim lockItem, LockTypes lockType)
		{
			_lock = lockItem;
			_lockType = lockType;

			switch (_lockType)
			{
				case LockTypes.ReaderLock:
					_lock.EnterReadLock();
					break;
				case LockTypes.WriterLock:
					_lock.EnterWriteLock();
					break;
			}
		}
		public void Dispose()
		{
			if (_lock == null)
				return;

			
			switch (_lockType)
			{
				case LockTypes.ReaderLock:
					_lock.ExitReadLock();
					break;
				case LockTypes.WriterLock:
					_lock.ExitWriteLock();
					break;
			}
		}
	}
	public class ReaderLockHelper : ReaderWriterLockSlimHelper
	{
		public ReaderLockHelper(ReaderWriterLockSlim lockItem) : base(lockItem, LockTypes.ReaderLock)
		{
		}
	}
	public class WriterLockHelper : ReaderWriterLockSlimHelper
	{
		public WriterLockHelper(ReaderWriterLockSlim lockItem)
			: base(lockItem, LockTypes.WriterLock)
		{
		}
	}
}
